﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Auth.Service;
using DotNet.Edu.Entity;
using DotNet.Edu.Service;
using DotNet.Edu.WebUtility;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.Utility;

namespace DotNet.StudentWap.Controllers
{
    [DomainLogIn]
    public class StudentLearningController : StudentWapController
    {
        public ActionResult CheckPeriod(string coursewareId)
        {
            var session = CurrentStudent;
            var student = session.Student;
            var studentId = student.Id;

            if (student.Status == 3)
            {
                return Json(new BoolMessage(false, "你正在准备考试,学习将不计时"));
            }
            if (student.Status == 4)
            {
                return Json(new BoolMessage(false, "你正在考试中,学习将不计时"));
            }
            if (student.Status == 5)
            {
                return Json(new BoolMessage(false, "你已完成本次继续教育,学习将不计时"));
            }

            int eduDayMaxPeriod = CurrentEduCategory.EduDayMaxPeriod; //AuthService.Param.Get("EduDayMaxPeriod", "14400").ToInt();
            int studentDayMaxPeriod = EduService.PeriodDetails.GetStudentDayPeriod(studentId, DateTime.Now.Date);
            if (studentDayMaxPeriod >= eduDayMaxPeriod)
            {
                return Json(new BoolMessage(false, "你已经完成今天最大学习时长,学习将不计时"));
            }

            int eduCoursewarePeriod = EduService.Courseware.Get(coursewareId).Period;
            int studentCoursewarePeriod = EduService.StudentCoursewarePeriod.GetStudentCoursewarePeriod(studentId, coursewareId);
            if (studentCoursewarePeriod >= eduCoursewarePeriod)
            {
                return Json(new BoolMessage(false, "本课件已经学习完成,学习将不计时"));
            }

            int eduTotalPeriod = CurrentEduCategory.EduTotalPeriod; //AuthService.Param.Get("EduTotalPeriod", "72000").ToInt();
            int studentTotalPeriod = student.TotalPeriod;
            if (studentTotalPeriod >= eduTotalPeriod)
            {
                return Json(new BoolMessage(false, "课件已经全部完成,学习将不计时"));
            }

            return Json(BoolMessage.True);
        }

        public ActionResult Begin(string coursewareId)
        {
            //检测今天完成的学时数字
            var session = CurrentStudent;
            var student = session.Student;
            PeriodDetails entity = new PeriodDetails();
            entity.CoursewareId = coursewareId;
            entity.SchoolId = student.SchoolId;
            entity.SchoolName = student.SchoolName;
            entity.StudentId = student.Id;
            entity.StudentName = student.Name;
            entity.StudyCategory = session.Device;
            entity.IPAddress = session.LoginIPAddr;
            entity.SignInDateTime = DateTime.Now;
            session.Learning = entity;
            return Json(BoolMessage.True);
        }

        public ActionResult Commit(int period)
        {
            var session = CurrentStudent;
            if (session.Learning != null)
            {
                var entity = session.Learning;
                //判断异常时长
                var sDateTime = entity.SignInDateTime;
                if ((DateTime.Now - sDateTime).TotalSeconds < period)
                {
                    return Json(new BoolMessage(false, "签到时间和学习时长不符,请勿非法提交"));
                }

                entity.SignOutDateTime = DateTime.Now;
                entity.Id = StringHelper.Guid();
                entity.Period = period;
                entity.CreateDateTime = DateTime.Now;
                entity.CreateDate = entity.CreateDateTime.Date;

                //如果学习时长超过课程规定的时长则 取课件规定时长
                var ylperiod = EduService.StudentCoursewarePeriod.GetStudentCoursewarePeriod(entity.StudentId, entity.CoursewareId);
                var gp = EduService.Courseware.GetPeriod(entity.CoursewareId);
                if ((entity.Period + ylperiod) >= gp)
                {
                    entity.Period = gp - ylperiod;
                }

                EduService.PeriodDetails.Create(entity);
                EduService.StudentCoursewarePeriod.Save(entity.StudentId, entity.CoursewareId, entity.Period);
                session.Student.TotalPeriod = EduService.StudentCoursewarePeriod.GetStudentPeriod(entity.StudentId);
                EduService.Student.UpdateTotalPeriod(entity.StudentId, session.Student.TotalPeriod, CurrentEduCategory.EduTotalPeriod);
                return Json(BoolMessage.True);
            }
            return Json(new BoolMessage(false, "请先进行学前验证"));
        }

        public ActionResult LearningValidateModal()
        {
            var entity = EduService.StudentValidate.GetRandom();
            var list = new List<string>();
            if (entity.A.IsNotEmpty()) list.Add(entity.A);
            if (entity.B.IsNotEmpty()) list.Add(entity.B);
            if (entity.C.IsNotEmpty()) list.Add(entity.C);
            if (entity.D.IsNotEmpty()) list.Add(entity.D);
            return Json(new { id = entity.Id, name = entity.Name, options = RandomHelper.RandomArray(list.ToArray()) });
        }

        [HttpPost]
        public ActionResult LearningValidate(string validateId, string coursewareId, string answer, int validTotals, int recordType)
        {
            var result = false;
            var validEntity = EduService.StudentValidate.Get(validateId);
            if (validEntity.Answer.Equals(answer, StringComparison.OrdinalIgnoreCase))
            {
                result = true;
            }
            var log = new LessonLog();
            log.Id = StringHelper.Guid();
            log.StudentId = CurrentStudent.StudentId;
            log.CoursewareId = coursewareId;
            log.CoursewareName = EduService.Courseware.GetName(coursewareId);
            log.WorkType = CurrentStudent.Student.WorkCategoryId;
            log.WorkTypeName = CurrentStudent.Student.WorkCategoryName;
            log.CreateDateTime = DateTime.Now;
            log.RecordType = recordType;
            log.Result = result;
            EduService.LessonLog.Create(log);
            if (result && recordType == 3)
            {
                PeriodSave(coursewareId, validTotals);
            }
            return Json(new BoolMessage(result));
        }

        private BoolMessage PeriodSave(string coursewareId, int period)
        {
            var session = CurrentStudent;
            var student = session.Student;
            PeriodDetails entity = new PeriodDetails();
            entity.Id = StringHelper.Guid();
            entity.CoursewareId = coursewareId;
            entity.SchoolId = student.SchoolId;
            entity.SchoolName = student.SchoolName;
            entity.StudentId = student.Id;
            entity.StudentName = student.Name;
            entity.StudyCategory = session.Device;
            entity.IPAddress = session.LoginIPAddr;
            entity.SignInDateTime = DateTime.Now;


            entity.SignOutDateTime = DateTime.Now;
            entity.Period = period;
            entity.CreateDateTime = DateTime.Now;
            entity.CreateDate = entity.CreateDateTime.Date;

            //如果学习时长超过课程规定的时长则 取课件规定时长
            var ylperiod = EduService.StudentCoursewarePeriod.GetStudentCoursewarePeriod(entity.StudentId, entity.CoursewareId);
            var gp = EduService.Courseware.GetPeriod(entity.CoursewareId);
            if ((entity.Period + ylperiod) >= gp)
            {
                entity.Period = gp - ylperiod;
            }

            EduService.PeriodDetails.Create(entity);
            EduService.StudentCoursewarePeriod.Save(entity.StudentId, entity.CoursewareId, entity.Period);
            session.Student.TotalPeriod = EduService.StudentCoursewarePeriod.GetStudentPeriod(entity.StudentId);
            EduService.Student.UpdateTotalPeriod(entity.StudentId, session.Student.TotalPeriod, CurrentEduCategory.EduTotalPeriod);
            entity.SignInDateTime = DateTime.Now;
            return BoolMessage.True;
        }


        [AllowAnonymous]
        public ActionResult ValidateImage(string validateId)
        {
            var validEntity = EduService.StudentValidate.Get(validateId);
            var msg = validEntity.Name;
            ValidateCodeDrawHelper v = new ValidateCodeDrawHelper();
            v.FontSize = 12;
            v.Padding = 5;
            var bmp = v.CreateImage(msg,false);
            return File(ImageHelper.ToArray(bmp), "image/png");
        }
    }
}